package stack;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Stack;

public class Test2 {
    public static void main(String[] args) {
       MyStack myStack= new MyStack();
       myStack.push(55);
       myStack.push(33);
       myStack.push(15);
       int ret=myStack.pop();//15
        System.out.println(ret);
        int ret2=myStack.peek();//33
        System.out.println(ret2);
    }
    public static void main2(String[] args) {
        Stack<Integer> stack=new Stack<>();
        stack.push(12);
        stack.push(56);
        stack.push(59);
        stack.push(23);
        stack.push(85);
        int ret=stack.pop();
        System.out.println(ret);
        int ret2=stack.peek();//23
        System.out.println(ret2);
    }
    public static void main1(String[] args) {
        LinkedList<Integer> list=new LinkedList<>();
        list.add(56);
        list.add(59);
        list.add(47);
        for (int i = 0; i <list.size(); i++) {
           int  ret= list.get(i);
            System.out.print(ret+" ");
        }
        System.out.println();
        System.out.println("==========");
        for (Integer x:list){
            System.out.print(x+" ");
        }
        System.out.println();
        System.out.println("===========");
        Iterator<Integer> it=list.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("============");
        ListIterator<Integer> listIterator =list.listIterator();
        while (listIterator.hasNext()){
            System.out.println(listIterator.next()+" ");
        }
        System.out.println();
        System.out.println("=============");
        ListIterator<Integer> listIterator2 =list.listIterator(list.size());
        while (listIterator2.hasPrevious()){
            System.out.println(listIterator2.previous()+" ");
        }
        System.out.println();
        System.out.println("=============");
    }
    //括号匹配问题
    class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack=new Stack<>();
            for(int i=0;i<s.length();i++){
                char ch=s.charAt(i);
                //1.如果拿到的字符是左括号，就push进栈中先存起来
                if(ch=='('||ch=='['||ch=='{'){
                    stack.push(ch);
                }
                //2.不走上面的if语句，就说明拿到的是右括号
                else{
                    //2.1拿到的是右括号，并且栈是空的
                    if(stack.empty()){
                        return false;
                    }
                    //2.2拿到的是右括号，但是栈不是空的
                    char ch2=stack.peek();//看栈顶的元素是哪一个 '('  '['  '{'
                    //2.2.1匹配
                    if(ch==')'&&ch2=='('||ch==']'&&ch2=='['||ch=='}'&&ch2=='{'){
                        stack.pop();
                    }
                    //2.2.2不匹配
                    else{
                        return false;
                    }
                }
            }
            if(!stack.empty()){
                return false;
            }
            return true;
        }
    }
    //判断出栈顺序是否正确
        public boolean IsPopOrder (int[] pushV, int[] popV) {
            // write code here
            Stack<Integer> stack=new Stack<>();
            int j=0;
            for(int i=0;i<pushV.length;i++){
                stack.push(pushV[i]);
                while(!stack.empty()&&j<popV.length&&stack.peek()==popV[j]){
                    j++;
                    stack.pop();
                }
            }
            return stack.empty();
        }
   //逆波兰表达式求值
        public int evalRPN(String[] tokens) {
            Stack<Integer> stack=new Stack<>();
            for(String str:tokens){
                if(!isOperation(str)){
                    //1.是数字,压进栈
                    stack.push(Integer.parseInt(str));
                }
                else{
                    //2.是操作数
                    int val2=stack.pop();
                    int val1=stack.pop();
                    switch(str){
                        case "+":
                            stack.push(val1+val2);
                            break;
                        case "-":
                            stack.push(val1-val2);
                            break;
                        case "*":
                            stack.push(val1*val2);
                            break;
                        case "/":
                            stack.push(val1/val2);
                            break;
                    }
                }
            }
            return stack.pop();

        }
        public boolean isOperation(String s){
            if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
                return true;
            }else{
                return false;
            }
        }

    }
    

